package net.huanger.gametools.mapeditor.ui.impl;


import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;

import net.huanger.gametools.mapeditor.model.MapModel;
import net.huanger.gametools.mapeditor.ui.MUtils;
import net.huanger.gametools.mapeditor.ui.MapCanvas;
import net.huanger.gametools.mapeditor.ui.MapEditorPanel;
import net.huanger.gametools.mapeditor.ui.TitePicCanvas;


public class PuzzleOperate extends AbstractDataOperate
{

  private int oldTiteCell = -1;

  private BufferedImage titeImage;

  private TitePicCanvas titePicCanvas;

  public PuzzleOperate(int row, int column)
  {
    super(row, column);
  }

  @Override
  public void init()
  {
  }

  @Override
  public void titeClick(MouseEvent e, int row, int column)
  {
    int selectCell = MUtils.pointToCell(e.getPoint(), row, column);
    if (selectCell != MUtils.OUT_LIMIT)
    {
      //如果与上次相同，这直接返回
      if (selectCell == oldTiteCell)
        return;
      Point old = MUtils.cellToPoint(oldTiteCell, row, column);
      oldTiteCell = selectCell;
      Point titePoint = MUtils.cellToPoint(selectCell, row, column);
      if (titePicCanvas == null)
      {
        titePicCanvas = (TitePicCanvas)e.getSource();
      }
     setTiteImage(titePicCanvas.getTiteImage().getSubimage(titePoint.x, titePoint.y, MUtils.MAP_UNIT, MUtils.MAP_UNIT));
      
      titePicCanvas.repaint(old, titePoint);
    }
  }

  @Override
  public void mapClick(MouseEvent e)
  {
    if (getTiteImage() != null)
    {
      int pointToCell = MUtils.pointToCell(e.getPoint(), this.mapRow, this.mapColumn);
      if (pointToCell != MUtils.OUT_LIMIT)
      {
        Point draw = MUtils.cellToPoint(pointToCell, this.mapRow, this.mapColumn);
        if (SwingUtilities.isLeftMouseButton(e))
        {//左键绘图

          //如果同时按下shift键则为快速取tite
          if (e.isShiftDown())
          {
            quickSelectTite(getCellData(pointToCell));
            return;
          }

          //如果data与从前一致，直接返回
          if (getCellData(pointToCell) == oldTiteCell)
            return;
          Graphics g = getGraphics();
          g.drawImage(getTiteImage(), draw.x, draw.y, null);
          //设置data
          setCellData(pointToCell, oldTiteCell);
        }
        else if (SwingUtilities.isRightMouseButton(e))
        {//右键清除
         //如果data与从前一致，直接返回
          if (getCellData(pointToCell) == 0)
            return;
          getImage().setRGB(draw.x, draw.y, MUtils.MAP_UNIT, MUtils.MAP_UNIT, MUtils.TRANSPARENT_DATA, 0, MUtils.MAP_UNIT);
          setCellData(pointToCell, 0);
        }
        MapCanvas mapCanvas = (MapCanvas)e.getSource();
        mapCanvas.repaint(draw.x, draw.y, MUtils.MAP_UNIT, MUtils.MAP_UNIT);
      }
    }
  }

  private void quickSelectTite(int selectCell)
  {
    if (selectCell == 0)
      return;
    //如果与上次相同，这直接返回
    if (selectCell == oldTiteCell)
      return;
    int row = titePicCanvas.getRow();
    int column = titePicCanvas.getColumn();
    Point old = MUtils.cellToPoint(oldTiteCell, row, column);
    oldTiteCell = selectCell;
    Point titePoint = MUtils.cellToPoint(selectCell, row, column);
    setTiteImage(titePicCanvas.getTiteImage().getSubimage(titePoint.x, titePoint.y, MUtils.MAP_UNIT, MUtils.MAP_UNIT));
    titePicCanvas.repaint(old, titePoint);
  }

  public BufferedImage getTiteImage()
  {
    return titeImage;
  }

  public void setTiteImage(BufferedImage titeImage)
  {
    MapEditorPanel.TITELABEL.setIcon(new ImageIcon(titeImage));
    this.titeImage = titeImage;
  }

  @Override
  public void restoreData(MapModel mm)
  {
    // TODO 拼图还原
    if(TitePicCanvas.image==null)return;
    int[]data=mm.getTite();
    int titeRow=mm.getTiteRow(),titeColumn=mm.getTiteColumn();
    for(int i=0;i<data.length;i++){
      if(data[i]==0)continue;
        Point dp=MUtils.cellToPoint(i, mapRow, mapColumn);
        Point sp=MUtils.cellToPoint(data[i], titeRow, titeColumn);
        Graphics g=getGraphics();
        g.drawImage(TitePicCanvas.image, dp.x, dp.y, dp.x+MUtils.MAP_UNIT, dp.y+MUtils.MAP_UNIT, sp.x, sp.y, sp.x+MUtils.MAP_UNIT, sp.y+MUtils.MAP_UNIT, null);
        setCellData(i, data[i]);
    }
  }
}
